// Written by Dale J Williams.
// Licensed under MPL 1.1 unless otherwise specified. 
// http://www.mozilla.org/MPL/MPL-1.1.html

package engineDJW {
	
	import engineDJW.input.KeyboardManager;
	import engineDJW.load.LoadManager;
	import engineDJW.screens.ScreenManager;
	import engineDJW.sound.SoundManager;
	import engineDJW.tween.TweenManager;
	import flash.display.Sprite;
	import flash.display.Stage;
	/**
	 * ...
	 * @author Dale J Williams
	 */
	public class GameCore {
		
		static private var _loader		:LoadManager 		= new LoadManager();
		static private var _tween		:TweenManager		= new TweenManager();
		static private var _input		:KeyboardManager;
		static private var _screen		:ScreenManager;
		static private var _sound		:SoundManager;
		static private var _engine		:GameEngine;
		static private var _stage		:Stage;
		static private var _cont		:Sprite;
		static private var _overlay		:Sprite;
		static private var _settings	:XML;
		static private var _path		:String;
		static private var _complete	:Boolean;
		
		static private var _onComplete	:Function;
		
		static public function init($stage:Stage, $settings:XML, $path:String=null):void {
			_stage = $stage;
			_settings = $settings;
			_path = $path || "";
			Main.self.addChild(_cont = new Sprite());
			Main.self.addChild(_overlay = new Sprite());
			
			_input = new KeyboardManager(_stage);
			_screen = new ScreenManager(_cont);
			_sound = new SoundManager();
			
			if (_settings..@file.length() > 0) load();
			else loadComplete();
		}
		
		static private function load():void {
			for each(var node:XML in _settings..@file) {
				node = node.parent();
				loader.addToCue(_path + node.@file, node.@name);
			}
			loader.onComplete = loadComplete;
			loader.load();
		}
		
		static private function loadComplete():void {
			if (_onComplete != null) _onComplete();
		}
		
		static public function get stage():Stage { return _stage; }
		
		static public function get path():String { return _path; }
		
		static public function get complete():Boolean { return _complete; }
		
		static public function get settings():XML { return _settings; }
		
		static public function get onComplete():Function { return _onComplete; }		
		static public function set onComplete(value:Function):void {
			_onComplete = value;
		}
		
		static public function get loader():LoadManager { return _loader; }
		
		static public function get screen():ScreenManager { return _screen; }
		
		static public function get input():KeyboardManager { return _input; }
		
		static public function get width():Number {
			return _stage.stageWidth;
		}
		
		static public function get height():Number {
			return _stage.stageHeight;
		}
		
		static public function get tween():TweenManager { return _tween; }

		static public function get engine():GameEngine { return _engine; }		
		static public function set engine(value:GameEngine):void {
			_engine = value;
		}
		
		static public function get sound():SoundManager { return _sound; }
		
		static public function get overlay():Sprite { return _overlay; }
		
	}

}